<?php

namespace app\controller;

use app\BaseController;
use think\App;
use think\helper\Str;
use think\response\Json;

class Upload extends BaseController
{
    protected $file;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->file = $this->request->file($this->request->param("file_name"));
    }

    public function uploadEditFile(): Json
    {
        try {
            $info = $this->app->make(Authorization::class)->checkAdminLogin();
            if ($info->getCode() != 200) throw new \Exception("请登录", 7004);
            if (empty($this->file)) throw new \Exception("请上传文件", 7005);
            $dir = "./upload/editor/" . date("Ymd");
            if (!is_dir($dir)) mkdir($dir, 0777, true);
            $file_name = $this->fieldName();
            $this->file->move($dir, $file_name);
            return json([
                "errno" => 0,
                "data" => [
                    "url" => realDomain() . "/upload/editor/" . date("Ymd") . "/$file_name",
                    "alt" => $this->file->getFilename()
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                "errno" => $e->getCode(),
                "message" => $e->getMessage()
            ], 401);
        }
    }

    public function product_image(): Json
    {
        try {
            $info = $this->app->make(Authorization::class)->checkAdminLogin();
            if ($info->getCode() != 200) throw new \Exception("请登录", 7004);
            if (empty($this->file)) throw new \Exception("请上传文件", 7005);
            $dir = "./upload/product/" . date("Ymd");
            if (!is_dir($dir)) mkdir($dir, 0777, true);
            $file_name = $this->fieldName();
            $this->file->move($dir, $file_name);
            $path = "$dir/$file_name";
            $url = realDomain() . "/upload/product/" . date("Ymd") . "/$file_name";
            return $this->success([
                "path" => $path,
                "url" => $url
            ]);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), $e->getCode());
        }
    }

    public function readExcelData(): Json
    {
        try {
            $info = $this->app->make(Authorization::class)->checkAdminLogin();
            if ($info->getCode() != 200) throw new \Exception("请登录", 7004);
            if (empty($this->file)) throw new \Exception("请上传文件", 7005);
            $dir = "./upload/excel/" . date("Ymd");
            if (!is_dir($dir)) mkdir($dir, 0777, true);
            $file_name = $this->fieldName();
            $this->file->move($dir, $file_name);
            //券码	卡密
            $data = readExcel($dir . "/" . $file_name,["券码"=>'code',"卡密"=>'pwd']);
            @unlink($dir . "/" . $file_name);
            return $this->success($data);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), $e->getCode());
        }
    }

    protected function fieldName(): string
    {
        return md5($this->file->getFilename() . Str::random(16)) . "." . $this->file->extension();
    }

}
